package com.ruoyi.system.controller;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ArchiveUsageStats;
import com.ruoyi.system.service.ArchiveUsageStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 档案利用人次件次统计 Controller
 * 提供接口给前端调用，实现前后端交互
 *
 * @author [你的名字或团队名]
 */
@RestController
@RequestMapping("/archive/usageStats")
public class ArchiveUsageStatsController {

    @Autowired
    private ArchiveUsageStatsService statsService;

    /**
     * 查询档案利用人次件次统计列表
     * 支持名称、月份等条件查询
     */
    @GetMapping("/list")
    public AjaxResult list(ArchiveUsageStats stats) {
        List<ArchiveUsageStats> list = statsService.selectArchiveUsageStatsList(stats);
        List<String> names = new ArrayList<>();
        List<Integer> seriesData1 = new ArrayList<>();
        List<Integer> seriesData2 = new ArrayList<>();
        if(list != null && list.size() > 0)
        {
            names = list.stream().map(m -> m.getMonth()+"月").distinct().collect(Collectors.toList());
        }
        for (String name : names) {
            List<ArchiveUsageStats> collect = list.stream().filter(f -> (f.getMonth() + "月").equals(name)).collect(Collectors.toList());
            if(collect != null && collect.size() > 0)
            {
                for (ArchiveUsageStats archiveUsageStats : collect) {
                    if(archiveUsageStats.getName().equals("人次"))
                    {
                        if(archiveUsageStats.getValue() != null)
                        {
                            seriesData1.add(archiveUsageStats.getValue());
                        }else{
                            seriesData1.add(0);
                        }
                    }
                    if(archiveUsageStats.getName().equals("件次"))
                    {
                        if(archiveUsageStats.getValue() != null)
                        {
                            seriesData2.add(archiveUsageStats.getValue());
                        }else{
                            seriesData2.add(0);
                        }
                    }
                }
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("seriesData",list);
        //人次
        map.put("seriesData1",seriesData1);
        //件次
        map.put("seriesData2",seriesData2);
        map.put("xData",names);
        return AjaxResult.success(map); // 封装分页结果
    }


    /**
     * 根据主键ID查询档案利用人次件次统计详情
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ArchiveUsageStats stats = statsService.selectArchiveUsageStatsById(id);
        return AjaxResult.success(stats);
    }

    /**
     * 新增档案利用人次件次统计记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody ArchiveUsageStats stats) {
        int result = statsService.insertArchiveUsageStats(stats);
        return AjaxResult.success(result);
    }

    /**
     * 修改档案利用人次件次统计记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ArchiveUsageStats stats) {
        int result = statsService.updateArchiveUsageStats(stats);
        return AjaxResult.success(result);
    }

    /**
     * 删除档案利用人次件次统计记录（单条）
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        int result = statsService.deleteArchiveUsageStatsById(id);
        return AjaxResult.success(result);
    }

    /**
     * 批量删除档案利用人次件次统计记录
     */
    @DeleteMapping("/batch/{ids}")
    public AjaxResult removeBatch(@PathVariable Long[] ids) {
        int result = statsService.deleteArchiveUsageStatsByIds(ids);
        return AjaxResult.success(result);
    }

    /**
     * 批量保存档案利用人次件次统计数据（先删后插）
     */
    @PostMapping("/batchSave")
    public AjaxResult batchSave(@RequestBody List<ArchiveUsageStats> list) {
        int result = statsService.batchSaveArchiveUsageStats(list);
        return AjaxResult.success(result);
    }
}